//
//  SNPreviewImageViewController.swift
//  SNVIPER
//
//  Created by shaofeng on 2017/5/19.
//  Copyright © 2017年 shaofeng. All rights reserved.
//

import UIKit

public typealias previewImageViewDidFinish = ([String]) ->();

public class SNPreviewImageViewController<T>: UIViewController,
UICollectionViewDelegateFlowLayout,
UICollectionViewDataSource,
UIScrollViewDelegate {

    public var dataList: Array<T> = [];
    public var currentIndex: Int = 0;
    private var isHideNaviBar: Bool = false;
    
    public var didFinish: previewImageViewDidFinish?;
    
    var collectionView: SNCustomCollectionView?;
    
    override public func viewDidLoad() {
        super.viewDidLoad()
        self.automaticallyAdjustsScrollViewInsets = false;
        let layout = UICollectionViewFlowLayout();
        layout.minimumLineSpacing = 0.0;
        layout.minimumInteritemSpacing = 0.0;
        layout.scrollDirection = .horizontal;
        layout.itemSize = CGSize(width: self.view.bounds.size.width + 20, height: self.view.bounds.size.height)
        collectionView = SNCustomCollectionView(frame: CGRect(x: -10, y: 0, width: self.view.bounds.size.width + 20, height: self.view.bounds.size.height), collectionViewLayout: layout);
        collectionView?.isPagingEnabled = true;
        collectionView?.register(SNPreviewImageCollectionViewCell.self, forCellWithReuseIdentifier: NSStringFromClass(SNPreviewImageCollectionViewCell.self));
        collectionView?.delegate = self;
        collectionView?.dataSource = self;
        collectionView?.contentOffset = CGPoint(x: (UIScreen.main.bounds.size.width + 20) * CGFloat(currentIndex), y: 0);
        collectionView?.contentSize = CGSize(width: (UIScreen.main.bounds.size.width + 20) * CGFloat(self.dataList.count), height: 0);
        self.view.addSubview(collectionView!);
    }

    override public func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    //MARK: UICollectionViewDataSource
    
    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataList.count;
    }

    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: NSStringFromClass(SNPreviewImageCollectionViewCell.self), for: indexPath) as! SNPreviewImageCollectionViewCell;
        cell.contentView.backgroundColor = UIColor.black;
        let data = dataList[indexPath.row];
        cell.setData(data: data);
        cell.singleTapGestureBlock = { [weak self] in
            let isHide = !(self?.isHideNaviBar ?? true);
            self?.isHideNaviBar = isHide;
            self?.navigationController?.navigationBar.isHidden = isHide;
        }
        return cell;
    }
    
    public func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        
        if let c = cell as? SNPreviewImageCollectionViewCell {
            c.recoverSubviews();
        }
    }
    
    public func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath){
        
        if let c = cell as? SNPreviewImageCollectionViewCell {
            c.recoverSubviews();
        }
    }
    
}
